<template>
    <view style="padding: 15px;">
        <u-form ref="validateFormRef" :model="validateForm.value" :label-width="150">
            <u-form-item label="新的邮箱" prop="email" required>
                <u-input v-model="validateForm.value.email" placeholder="请输入新的邮箱"/>
            </u-form-item>
            <u-form-item label="确认邮箱" prop="confirmEmail" required>
                <u-input v-model="validateForm.value.confirmEmail" placeholder="请输入确认邮箱"/>
            </u-form-item>
            <u-form-item label="验证码" prop="identifyingCode" required>
                <u-input placeholder="请输入验证码" v-model="validateForm.value.identifyingCode" type="text"></u-input>
                <u-button slot="right" type="success" size="mini" @click="getCode">{{codeTips}}</u-button>
            </u-form-item>
        </u-form>

        <u-button type="error" @click="submit">提交</u-button>
        <!--邮箱验证码-->
        <u-verification-code seconds="60" ref="uCode" @change="codeChange"></u-verification-code>
    </view>
</template>

<script>

    import {TbEmailIdentifyingCodeService} from "@/dev/services/tb_email_identifying_code.service";
    import {CommonUtil} from "@/dev/core/utils/common.util";
    import {SysUserService} from "@/dev/services/sys-user.service";
    import {LocalStorageUtil} from "@/dev/core/utils/local-storage.util";
    import {UFormUtil} from "@/dev/core/utils/u-form.util";

    export default {
        data() {
            return {
                codeTips: '',  // 获取验证码提示语
                validateForm: {
                    value: {
                        id: this.globalVariable.userInfo.id,
                        email: '',
                        confirmEmail: '',
                        identifyingCode: ''
                    },
                    rules: {
                        email: [
                            {required: true, type: "email", message: '请输入有效的邮箱号码', trigger: ['change', 'blur']},
                            {
                                trigger: ['blur'],
                                asyncValidator: (rule, value, callback) => {
                                    if (this.validateForm.value.confirmEmail) {
                                        UFormUtil.validation(this.$refs.validateFormRef, 'confirmEmail');
                                    }
                                    callback();
                                }
                            }
                        ],
                        confirmEmail: [
                            {required: true, type: "email", message: '请输入有效的邮箱号码', trigger: ['change', 'blur']},
                            {
                                trigger: ['blur'],
                                asyncValidator: (rule, value, callback) => {
                                    if (value !== this.validateForm.value.email) {
                                        callback(new Error('两次输入的邮箱不一致'))
                                    } else {
                                        callback();
                                    }
                                }
                            }
                        ],
                        identifyingCode: [
                            {required: true, message: '请输入邮箱验证码', trigger: ['change', 'blur']},
                            {
                                trigger: ['blur'],
                                asyncValidator: (rule, value, callback) => {
                                    new TbEmailIdentifyingCodeService().mailIdentifyingCodeCheck(this.validateForm.value.email, this.validateForm.value.identifyingCode).then(result => {
                                        if (result.status === "success") {
                                            callback()
                                        } else {
                                            callback(new Error('输入的邮箱验证码有误'));
                                        }
                                    });
                                }
                            }
                        ]
                    }
                }
            }
        },
        onReady() {
            const _self = this

        },
        methods: {
            codeChange(text) {
                this.codeTips = text;
            },
            // 获取验证码
            getCode() {
                const _self = this;
                if (this.$refs.uCode.canGetCode) {
                    // 模拟向后端请求验证码
                    uni.showLoading({
                        title: '正在获取验证码',
                        mask: true
                    });

                    this.validateForm.value.email = CommonUtil.trim(this.validateForm.value.email);
                    if (!this.validateForm.value.email) {
                        this.$u.toast('请先输入信的邮箱');
                        return;
                    }
                    this.validateForm.value.confirmEmail = CommonUtil.trim(this.validateForm.value.confirmEmail);
                    if (!this.validateForm.value.confirmEmail) {
                        this.$u.toast('请先输入确认邮箱');
                        return;
                    }
                    if (this.validateForm.value.email !== this.validateForm.value.confirmEmail) {
                        this.$u.toast('两次输入的邮箱不一致');
                        return;
                    }

                    new TbEmailIdentifyingCodeService().sendMailIdentifyingCode(this.validateForm.value.email).then(result => {
                        CommonUtil.alert(result.message);
                        uni.hideLoading();
                        // 通知验证码组件内部开始倒计时
                        _self.$refs.uCode.start();
                    });
                } else {
                    this.$u.toast('倒计时结束后再发送');
                }
            },
            submit() {
                const _self = this;
                this.$refs.validateFormRef.validate(valid => {
                    if (valid) {
                        console.log('验证通过', _self.validateForm.value);
                        CommonUtil.alert({
                            title: '提示！',
                            content: '确认提交吗？',
                            showCancel: true, // 是否显示取消按钮
                            success: function (res) {
                                if (res.cancel) { // 点击取消,默认隐藏弹框
                                } else { // 点击确定
                                    new SysUserService().updateSysUser(_self.validateForm.value).then(result => {
                                        Object.assign(_self.globalVariable.userInfo, CommonUtil.deleteNullAttrInObject(result));
                                        LocalStorageUtil.setItem("userInfo", _self.globalVariable.userInfo);
                                        CommonUtil.toastNavigateBack('更新成功', parseInt(_self.urlParams.backPageNum));
                                    });
                                }
                            }
                        });
                    } else {
                        console.log('验证失败');
                    }
                });
            }
        }
    }
</script>

<style lang="scss" scoped>
</style>
